/**create by liuhua at 2018年10月1日 下午5:08:29**/
package com.star.truffle.module.user.controller.api;

import java.util.List;

import javax.validation.Valid;
import javax.validation.constraints.NotBlank;
import javax.validation.constraints.NotNull;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.star.truffle.core.StarServiceException;
import com.star.truffle.core.annotation.IgnoreSecurity;
import com.star.truffle.core.eureka.StarGateway;
import com.star.truffle.core.web.ApiCode;
import com.star.truffle.core.web.ApiResult;
import com.star.truffle.module.user.domain.Area;
import com.star.truffle.module.user.dto.AreaLinked;
import com.star.truffle.module.user.service.AreaService;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;

@Slf4j
@Validated
@RestController
@RequestMapping("/api/area")
@Api(tags = "地区接口")
public class AreaApiController {

  @Autowired
  private AreaService areaService;
  
  @IgnoreSecurity
  @StarGateway
  @RequestMapping(value = "/list/by/parent", method = RequestMethod.POST)
  @ApiOperation(value = "获取下级地区列表", notes = "获取下级地区列表", httpMethod = "POST")
  @ApiImplicitParams({
    @ApiImplicitParam(name = "parentId", value = "父地区ID，省传0", dataType = "Long", required = true, paramType = "query")
  })
  public ApiResult<List<Area>> getAreasByParentId(@Valid @NotNull(message = "parentId 不能为空") Long parentId){
    try {
      List<Area> areas = this.areaService.getAreasByParentId(parentId);
      return ApiResult.success(areas);
    } catch (StarServiceException e) {
      return ApiResult.fail(e.getCode(), e.getMsg());
    } catch (Exception e) {
      log.error(e.getMessage(), e);
      return ApiResult.fail(ApiCode.SYSTEM_ERROR);
    }
  }
  
  @IgnoreSecurity
  @StarGateway
  @RequestMapping(value = "/info", method = {RequestMethod.POST, RequestMethod.GET})
  @ApiOperation(value = "获取地区信息", notes = "获取地区信息", httpMethod = "POST")
  @ApiImplicitParams({
    @ApiImplicitParam(name = "areaId", value = "地区ID", dataType = "Long", required = true, paramType = "query")
  })
  public ApiResult<Area> getAreaByAreaId(@Valid @NotNull(message = "areaId 不能为空") Long areaId){
    try {
      Area area = this.areaService.getAreaByAreaId(areaId);
      return ApiResult.success(area);
    } catch (StarServiceException e) {
      return ApiResult.fail(e.getCode(), e.getMsg());
    } catch (Exception e) {
      log.error(e.getMessage(), e);
      return ApiResult.fail(ApiCode.SYSTEM_ERROR);
    }
  }
  
  @IgnoreSecurity
  @StarGateway
  @RequestMapping(value = "/info/by/name", method = RequestMethod.POST)
  @ApiOperation(value = "根据地区名称获取地区信息", notes = "根据地区名称获取地区信息", httpMethod = "POST")
  @ApiImplicitParams({
    @ApiImplicitParam(name = "parentId", value = "父级id，省的父级id填0", dataType = "long", required = true, paramType = "query"),
    @ApiImplicitParam(name = "name", value = "地区名称", dataType = "String", required = true, paramType = "query"),
  })
  public ApiResult<Area> getAreaByAreaName(@Valid @NotNull(message = "areaId 不能为空") Long parentId, @NotBlank(message = "name 不能为空") String name){
    try {
      Area area = this.areaService.getAreaByAreaName(parentId, name);
      return ApiResult.success(area);
    } catch (StarServiceException e) {
      return ApiResult.fail(e.getCode(), e.getMsg());
    } catch (Exception e) {
      log.error(e.getMessage(), e);
      return ApiResult.fail(ApiCode.SYSTEM_ERROR);
    }
  }
  
  @IgnoreSecurity
  @StarGateway
  @RequestMapping(value = "/linked/by/name", method = RequestMethod.POST)
  @ApiOperation(value = "根据地区名称获取地区信息 及它的所有上级地区", notes = "根据地区名称获取地区信息 及它的所有上级地区", httpMethod = "POST")
  @ApiImplicitParams({
    @ApiImplicitParam(name = "parentId", value = "父级id，省的父级id填0", dataType = "long", required = true, paramType = "query"),
    @ApiImplicitParam(name = "name", value = "地区名称", dataType = "String", required = true, paramType = "query"),
  })
  public ApiResult<AreaLinked> getAreaLinkedByName(@Valid @NotNull(message = "parentId 不能为空") Long parentId, @NotBlank(message = "name 不能为空") String name){
    try {
      AreaLinked area = this.areaService.getAreaLinkedByName(parentId, name);
      return ApiResult.success(area);
    } catch (StarServiceException e) {
      return ApiResult.fail(e.getCode(), e.getMsg());
    } catch (Exception e) {
      log.error(e.getMessage(), e);
      return ApiResult.fail(ApiCode.SYSTEM_ERROR);
    }
  }
  
  @IgnoreSecurity
  @StarGateway
  @RequestMapping(value = "/linked/by/area", method = RequestMethod.POST)
  @ApiOperation(value = "根据地区id获取地区信息 及它的所有上级地区", notes = "根据地区id获取地区信息 及它的所有上级地区", httpMethod = "POST")
  @ApiImplicitParams({
    @ApiImplicitParam(name = "areaId", value = "地区id", dataType = "long", required = true, paramType = "query"),
  })
  public ApiResult<AreaLinked> getAreaLinkedByAreaId(@Valid @NotNull(message = "areaId 不能为空") Long areaId) {
    try {
      AreaLinked area = this.areaService.getAreaLinkedByAreaId(areaId);
      return ApiResult.success(area);
    } catch (StarServiceException e) {
      return ApiResult.fail(e.getCode(), e.getMsg());
    } catch (Exception e) {
      log.error(e.getMessage(), e);
      return ApiResult.fail(ApiCode.SYSTEM_ERROR);
    }
  }
}
